<template>
  <el-dialog
    width="500px"
    title="员工导入"
    :visible="showExcelDialog"
    @close="$emit('update:showExcelDialog', false)"
  >
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx, .xls"
          @change="handleChange"
        />
        <div class="drop">
          <i class="el-icon-upload" />
          <el-button type="text" @click="downloadTemplate"
            >下载导入模板</el-button
          >
          <span
            >将文件拖到此处或
            <el-button type="text" @click="uploadExcel">点击上传</el-button>
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <!-- update:props属性名，值 直接修改 .sync修饰符的属性值 -->
      <el-button
        size="mini"
        type="primary"
        @click="$emit('update:showExcelDialog', false)"
        >取消</el-button
      >
    </el-row>
  </el-dialog>
</template>
<script>
import { downloadImportExcelTemplate, uploadExcel } from "@/api/employee";
import FileSave from "file-saver";
export default {
  props: {
    showExcelDialog: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    // 下载模板
    async downloadTemplate() {
      // 1. 调接口数据模板数据
      this.$notify.info({
        title: "消息",
        message: "正在下载导入模板...",
      });
      const excelReasult = await downloadImportExcelTemplate();
      // 2. 把接收的数据存储成一个文件
      // console.log('ret1111',ret);
      FileSave.saveAs(excelReasult, "员工信息导入模板.xlsx");
    },
    // 上传文件
    uploadExcel() {
      // 1. 打开文件选择框
      this.$refs["excel-upload-input"].click();
    },
    // 2. 用户选中文件
    async handleChange(event) {
      try {
        //   console.log("event", [event.target]);
        const file = event.target.files[0];
        if (!file) return;
        // 3. 调接口上传文件
        const formData = new FormData();
        //   axiso这个Ajax工具，会识别我们传递的数据格式
        // 1. 比如我们默认传JSON对象，它就把请求头中的文件类型Content-Type声明成application/json格式
        // 2.如果传递formData对象，它就把请求头中的文件类型Content-Type声明成multipart/form-data格式
        formData.append("file", file);
        await uploadExcel(formData);
        //   4. 提示结果（成功&失败）
        this.$message.success("上传文件成功");
        // 5. 前端同步最新的员工列表数据
        this.$emit("updata-excel-list");
        // 6. 关弹窗
        this.$emit("updata:showExcelDialog", false);
      } finally {
        this.$refs["excel-upload-input"].value = ''
      }
    },
  },
};
</script>

<style scoped lang="scss">
.upload-excel {
  display: flex;
  justify-content: center;
  margin: 20px;
  width: 360px;
  height: 180px;
  align-items: center;
  color: #697086;
  .excel-upload-input {
    display: none;
    z-index: -9999;
  }
  .btn-upload,
  .drop {
    border: 1px dashed #dcdfe6;
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 160px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .drop {
    line-height: 40px;
    color: #bbb;
    i {
      font-size: 60px;
      display: block;
      color: #c0c4cc;
    }
  }
}
</style>
